package com.gopay.bis.web.controller.cbrmb;

import java.util.Enumeration;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gopay.bis.service.bank.cbrmb.MasaPayService;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayOrderNotifyDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayRefundOrderNotifyDTO;

/**
 * 接受外卡通知
 * 
 * @ClassName: MasaPayNotifyController
 * @Description: TODO
 * @author gd.cui
 * @date 2017年12月4日 下午1:22:08
 */
@Scope("prototype")
@Controller
@RequestMapping("/cbrmb/notify/")
public class MasaPayNotifyController {
	private static Log logger = LogFactory.getLog(MasaPayNotifyController.class);

	public static final String OK = "OK";
	public static final String GET = "GET";

	@Resource(name = "masaPayService")
	private MasaPayService masaPayService;

	@RequestMapping(value = "orderNoitfy")
	@ResponseBody
	public String orderNoitfy(HttpServletRequest request, HttpServletResponse response) {
		logger.info("--------------- MasaPayNotifyController orderNoitfy ---------------");
		if (GET.equals(request.getMethod().toUpperCase())) {
			return "ok test";
		} else {
			this.displayRequestParameters(request, logger);
			MasaPayOrderNotifyDTO orderNotify = getMasaPayOrderNotifyDTO(request);
			String result = masaPayService.orderNoitfy(orderNotify);
			if (OK.equalsIgnoreCase(result)) {
				return OK;
			} else {
				return null;
			}
		}
	}

	@RequestMapping(value = "refundOrderNoitfy")
	@ResponseBody
	public String refundOrderNoitfy(HttpServletRequest request, HttpServletResponse response) {
		logger.info("--------------- MasaPayNotifyController refundOrderNoitfy ---------------");
		if (GET.equals(request.getMethod().toUpperCase())) {
			return "ok test";
		} else {
			this.displayRequestParameters(request, logger);
			MasaPayRefundOrderNotifyDTO refundOrderNotify = getMasaPayRefundOrderNotifyDTO(request);
			String result = masaPayService.refundOrderNoitfy(refundOrderNotify);
			if (OK.equalsIgnoreCase(result)) {
				return OK;
			} else {
				return null;
			}
		}
	}

	private MasaPayOrderNotifyDTO getMasaPayOrderNotifyDTO(HttpServletRequest request) {
		MasaPayOrderNotifyDTO notifyOrder = new MasaPayOrderNotifyDTO();
		notifyOrder.setVersion(request.getParameter("version"));
		notifyOrder.setMerchantId(request.getParameter("merchantId"));
		notifyOrder.setCharset(request.getParameter("charset"));
		notifyOrder.setLanguage(request.getParameter("language"));
		notifyOrder.setSignType(request.getParameter("signType"));
		notifyOrder.setServiceType(request.getParameter("serviceType"));
		// business
		notifyOrder.setMerchantOrderNo(request.getParameter("merchantOrderNo"));
		notifyOrder.setGatewayOrderNo(request.getParameter("gatewayOrderNo"));
		notifyOrder.setSubmitTime(request.getParameter("submitTime"));
		notifyOrder.setDealTime(request.getParameter("dealTime"));
		notifyOrder.setCurrencyCode(request.getParameter("currencyCode"));
		notifyOrder.setOrderAmount(request.getParameter("orderAmount"));
		notifyOrder.setPaidAmount(request.getParameter("paidAmount"));
		notifyOrder.setResultCode(request.getParameter("resultCode"));
		notifyOrder.setErrCode(request.getParameter("errCode"));
		notifyOrder.setErrMsg(request.getParameter("errMsg"));
		notifyOrder.setRiskLevel(request.getParameter("riskLevel"));
		notifyOrder.setRiskDesc(request.getParameter("riskDesc"));
		notifyOrder.setExt1(request.getParameter("ext1"));
		notifyOrder.setExt2(request.getParameter("ext2"));
		notifyOrder.setSignMsg(request.getParameter("signMsg"));
		return notifyOrder;
	}

	private MasaPayRefundOrderNotifyDTO getMasaPayRefundOrderNotifyDTO(HttpServletRequest request) {
		MasaPayRefundOrderNotifyDTO refundOrderNotify = new MasaPayRefundOrderNotifyDTO();
		refundOrderNotify.setVersion(request.getParameter("version"));
		refundOrderNotify.setCharset(request.getParameter("charset"));
		refundOrderNotify.setLanguage(request.getParameter("language"));
		refundOrderNotify.setSignType(request.getParameter("signType"));
		refundOrderNotify.setServiceType(request.getParameter("serviceType"));
		// business
		refundOrderNotify.setRefundOrderNo(request.getParameter("refundOrderNo"));
		refundOrderNotify.setMerchantOrderNo(request.getParameter("merchantOrderNo"));
		refundOrderNotify.setGatewayRefundOrderNo(request.getParameter("gatewayRefundOrderNo"));
		refundOrderNotify.setSubmitTime(request.getParameter("submitTime"));
		refundOrderNotify.setDealTime(request.getParameter("dealTime"));
		refundOrderNotify.setCurrencyCode(request.getParameter("currencyCode"));
		refundOrderNotify.setRefundAmount(request.getParameter("refundAmount"));
		refundOrderNotify.setResultCode(request.getParameter("resultCode"));
		refundOrderNotify.setPayMode(request.getParameter("payMode"));
		refundOrderNotify.setOrgCode(request.getParameter("orgCode"));
		refundOrderNotify.setExt1(request.getParameter("ext1"));
		refundOrderNotify.setExt2(request.getParameter("ext2"));
		refundOrderNotify.setErrCode(request.getParameter("errCode"));
		refundOrderNotify.setErrMsg(request.getParameter("errMsg"));
		refundOrderNotify.setSignMsg(request.getParameter("signMsg"));
		return refundOrderNotify;
	}

	/**
	 * 输出请求参数
	 * 
	 * @param request
	 */
	@SuppressWarnings("rawtypes")
	protected void displayRequestParameters(HttpServletRequest request, Log logger) {
		for (Enumeration enumeration = request.getParameterNames(); enumeration.hasMoreElements();) {
			String eleName = (String) enumeration.nextElement();
			String eleValue = request.getParameter(eleName);
			if (logger != null) {
				logger.error("map key: [" + eleName + "]，value: [" + eleValue + "]");
			}
		}
	}

}
